<?php

 
 Zend_Loader::loadClass("BaseDBRows");
 Zend_Loader::loadClass("InventoryReject");
 class InventoryRejects extends BaseDBRows
 {
 	protected function setUp()
 	{
 		$this->_tableName = InventoryReject::_tablename;
 	}	
	
	public function getOrgInvRejectsByOrgId($orgId,$tank="",$moid="",$rejectReason="",$dateFrom="",$dateTo="",$transType="")
	{
		Zend_Loader::loadClass("Tank");
		Zend_Loader::loadClass("OrganizationOrganism");
		Zend_Loader::loadClass("Organism");
		Zend_Loader::loadClass("OrganismAttribute");
		Zend_Loader::loadClass("Size");
		Zend_Loader::loadClass("RejectReason");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("ir.reject_id, t.name tank_name, o.moid, o.name, s.size, rr.reason, ir.reject_date, ir.last_transaction_source, ir.quantity, ir.currency_mark, ir.amount");
		$query->addFrom($this->_tableName . " ir ");
		$query->addJoin("," . Tank::_tablename . " t ");
		$query->addJoin("," . OrganizationOrganism::_tablename . " oo ");
		$query->addJoin("," . Organism::_tablename . " o ");
		$query->addJoin("," . OrganismAttribute::_tablename . " oa ");
		$query->addJoin("," . Size::_tablename . " s ");
		$query->addJoin("," . RejectReason::_tablename . " rr ");
		$query->addWhere("ir.tank_id = t.tank_id");
		$query->addWhere("ir.org_organism_id = oo.org_organism_id");
		$query->addWhere("oo.organism_id = o.organism_id");
		$query->addWhere("ir.organism_attribute_id = oa.organism_attribute_id");
		$query->addWhere("oa.size_code = s.size_code");
		$query->addWhere("ir.reject_reason_id = rr.reject_reason_id");
		$query->addWhere("ir.org_id = " . $orgId);
		
		// tank
		if(strlen($tank) > 0) {
			$query->addWhere("t.tank_id = " . $tank);
		}
		// moid
		if(strlen($moid) > 0) {
			$query->addWhere("o.moid = '" . $moid . "'");	
		}
		// reject reason
		if(strlen($rejectReason) > 0) {
			$query->addWhere(" ir.reject_reason_id = " . $rejectReason);
		}
		// dates from - to
		if(strlen($dateFrom) > 0 && strlen($dateTo) > 0) {
			$query->addWhere(" ir.reject_date between least('" . $dateFrom . "','" . $dateTo . "') and greatest('" . $dateFrom . "','" . $dateTo . "')" );	
		}
		elseif(strlen($dateFrom) > 0) {
			$query->addWhere(" ir.reject_date >= '" . $dateFrom . "'" );
		}
		elseif(strlen($dateTo) > 0) {
			$query->addWhere(" ir.reject_date <= '" . $dateTo . "'");
		}
		// transaction type
		if(strlen($transType) > 0) {
			if ($transType == '1') {
				$query->addWhere(" ir.last_transaction_source is null ");
			} else {
				$query->addWhere(" ir.last_transaction_source = '" . $transType . "'");
			}
		}
		
		$db = DBCon::instance();
		// echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}

 }
?>